﻿#region Header

// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------

#endregion

#region Usings

using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using ScrumTable.Common.FileSystem;
using ScrumTable.Common.Logging;

#endregion

namespace ScrumTable.Common.Image
{
    /// <summary>
    /// Represents an utility class for image handling.
    /// </summary>
    public static class ImageUtil
    {
        #region Declarations

        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        
        #endregion

        #region Constructors / Destructor

        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods

        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns the image Base64 converted.
        /// </summary>
        /// <param name="image">Image to convert to a Base64 string.</param>
        /// <returns>Returns the converted image.</returns>
        public static string ToLineBreakString(this System.Drawing.Image image)
        {
            ImageConverter ic = new ImageConverter();
            byte[] buffer = (byte[])ic.ConvertTo(image, typeof(byte[]));
            return Convert.ToBase64String(buffer, Base64FormattingOptions.InsertLineBreaks);
        }
        
        /// <summary>
        /// Gets the image instance for the given Base64 string.
        /// </summary>
        /// <param name="base64String">String which contains the image information.</param>
        /// <returns>Returns the parsed and interpreted image.</returns>
        public static System.Drawing.Image FromLineBreakString(string base64String)
        {
            if (string.IsNullOrEmpty(base64String))
                return null;

            byte[] buffer = Convert.FromBase64String(base64String);
            ImageConverter ic = new ImageConverter();
            return (ic.ConvertFrom(buffer) as System.Drawing.Image);
        }


        /// <summary>
        /// Loads an Image from a File
        /// </summary>
        /// <param name="file">filename</param>
        /// <returns></returns>
        public static System.Drawing.Image GetImageFromFile(Uri file)
        {
            System.Drawing.Image image;
            try
            {
                
                if (File.Exists(file.ToLocalPath()))
                {
                    image = System.Drawing.Image.FromFile(file.ToLocalPath());
                }
                else
                {
                   image = System.Drawing.Image.FromFile(DataFolderUtil.GetDataFile(file.ToLocalPath()));
                   
                }
            }
            catch (IOException error)
            {
                Logger.Warn(String.Format(Properties.Resources.LogImageLoadingError, file.ToLocalPath(), error));
                image = null;
            }
            return image;
        }

        /// <summary>
        /// Loads an Image from a request
        /// </summary>
        /// <param name="request">webrequest to the image</param>
        /// <returns></returns>
        public static System.Drawing.Image GetImageFromRequest(WebRequest request)
        {
            System.Drawing.Image image;
            try
            {
                using (var response = request.GetResponse())
                {
                    image = System.Drawing.Image.FromStream(response.GetResponseStream());
                }
            }
            catch (Exception error)
            {
                Logger.Warn(String.Format(Properties.Resources.LogImageLoadingError, request.RequestUri), error);
                image = null;
            }
            return image;
        }


       
        #endregion

        #region Events

        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}